package org.catspaw.cherubim.persistence.dao.jdbc;

import org.catspaw.cherubim.persistence.mapping.EntityMappingResolver;

public class SqlUtils {

	public static String generateFindAllSql(EntityMappingResolver resolver) {
		return "select * from " + resolver.getTable();
	}
	public static String generateTotalSql(EntityMappingResolver resolver) {
		return "select count(*) from " + resolver.getTable();
	}

	public static String generateGetSql(EntityMappingResolver resolver) {
		return "select * from " + resolver.getTable() + " where "
				+ resolver.getIdColumn() + " = ?";
	}

	public static String generateSaveSql(EntityMappingResolver resolver) {
		String[] columns = resolver.getColumns();
		String sql = "insert into " + resolver.getTable() + "(";
		for (int i = 0; i < columns.length; i++) {
			sql += columns[i];
			if (i < columns.length - 1) {
				sql += ", ";
			}
		}
		sql += ") values(";
		for (int i = 0; i < columns.length; i++) {
			sql += "?";
			if (i < columns.length - 1) {
				sql += ", ";
			}
		}
		sql += ")";
		return sql;
	}

	public static String generateDeleteSql(EntityMappingResolver resolver) {
		return "delete from " + resolver.getTable() + " where "
				+ resolver.getIdColumn() + " = ?";
	}

	public static String generateUpdateSql(EntityMappingResolver resolver) {
		String sql = "update " + resolver.getTable() + " set ";
		String[] columns = resolver.getColumns();
		for (int i = 0; i < columns.length; i++) {
			sql += columns[i] + " = ?";
			if (i < columns.length - 1) {
				sql += ", ";
			}
		}
		sql += " where " + resolver.getIdColumn() + " = ?";
		return sql;
	}
}
